package lib.bone.access.module.dict.impl;

import cn.hutool.core.lang.Validator;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.dao.NepuAwardDictDAO;
import lib.bone.access.module.dict.entity.NepuAwardDict;
import lib.bone.access.module.dict.DictService;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class AwardDictServiceImpl implements DictService<NepuAwardDict,String> {
    @Autowired
    NepuAwardDictDAO dao;

//    @Autowired
//    NepuAwardDAO awardDAO;

    @Override
    public String getServiceType() {
        return ResearchTypeConst.AWARD;
    }

    @Override
    public PageResultDTO<NepuAwardDict> getAll(Pageable pageable, Example<NepuAwardDict> example) {
        Page<NepuAwardDict> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }else {
            page = dao.findAll(example,pageable);
        }
        List<NepuAwardDict> list = page.getContent();
        PageResultDTO<NepuAwardDict> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return pageResultDTO;
    }

    @Override
    public ResponseDTO<NepuAwardDict> deleteById(String id) {
//        Boolean useExist = awardDAO.existsByAwardDictId(id);
//        if(useExist){
//            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN,"存在关联该词典的奖励信息,拒绝执行删除!");
//        }
        Optional<NepuAwardDict> nepuAwardDictOp = getById(id);
        if(!nepuAwardDictOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"未找到ID:["+id+"]对应资源!");
        dao.deleteById(id);
        return ResponseDTO.succMsg("删除成功!");
    }

    @Override
    public Optional<NepuAwardDict> getById(String id) {
        return dao.findById(id);
    }

    @Override
    public NepuAwardDict saveOne(NepuAwardDict nepuAwardDict) {
        if (Validator.isNotEmpty(nepuAwardDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return dao.save(nepuAwardDict);
    }

    @Override
    public NepuAwardDict updateOne(NepuAwardDict nepuAwardDict) {
        if (Validator.isEmpty(nepuAwardDict.getId())){
            throw new BusinessException(ResponseCodeConst.ERROR_PARAM);
        }
        return dao.save(nepuAwardDict);
    }
}
